package com.softeem.god.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.god.comm.R;
import com.softeem.god.entity.Music;
import com.softeem.god.entity.MusicListDetails;
import com.softeem.god.service.MusicListDetailsService;
import com.softeem.god.service.MusicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.List;
@Tag(name = "音乐管理")
@RestController
public class MusicController {

    @Autowired
    private MusicService musicService;

    @Autowired
    private MusicListDetailsService musicListDetailsService;

    @Operation(summary = "音乐列表",description = "分页查询音乐列表")
    @Parameters(value = {@Parameter(name = "page",description = "当前页码"),
            @Parameter(name = "limit",description = "每页显示数量")})
    @GetMapping("/music/list")
    public R list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer limit){
        //创建分页对象 limint ？，？
        IPage<Music> ipage = new Page<>(page,limit);
        //执行查询(分页)
        List<Music> list = musicService.list(ipage);
        long count = musicService.count();
        //返回结果
        return R.success().setData(list).setCount(count);
    }
    @Operation(summary = "音乐详情",description = "查询指定id的音乐详情")
    @Parameters(value =
            {@Parameter(name = "id",description = "音乐id")
            })
    @GetMapping("/music/details")
    public R details(Integer id){
        return musicService.musicDetails(id);
    }
    @Operation(summary = "音乐添加",description = "添加音乐")
    @PostMapping("/music/add")
    public R add(@RequestBody Music music){
        boolean b = musicService.save(music);
        return b?R.success().setMsg("添加成功"):R.error().setMsg("添加失败");
    }
    @Operation(summary = "音乐删除",description = "删除指定id的音乐")
    @Parameters(value = {@Parameter(name = "id",description = "音乐id")})
    @DeleteMapping("/music/del")
    public R delete(Integer id){
        //根据id查询是否有关联的歌单
        //组合查询条件
        QueryWrapper< MusicListDetails> query = Wrappers.query(MusicListDetails.class);
        query.eq("music_id",id);
        //根据id查询是否有关联歌单详情数据
        List<MusicListDetails> list = musicListDetailsService.list(query);
        if(list.size()>0){
            return R.error().setMsg("删除失败，该音乐有歌单关联");
        }
        return musicService.removeById(id)?R.success().setMsg("删除成功"):R.error().setMsg("删除失败");
    }
}
